﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace ThanNhanTao.Server.BLL
{
    public class Dm_LoaiYeuCauBLL
    {
        bvdn_thannhantaoEntities entities;
        public Dm_LoaiYeuCauBLL()
        {
            entities = ThanNhanTaoGlobalServer.bvdn_thannhantaoEntities;
        }

        public List<Dm_LoaiYeuCau> RetrieveAllDmLoaiYeuCau()
        {
            List<Dm_LoaiYeuCau> listLoaiYeuCau = (from loaiYeuCau in entities.Dm_LoaiYeuCau
                                                  select loaiYeuCau).ToList();
            return listLoaiYeuCau;
        }

        public List<Dm_LoaiYeuCau> RetrieveAllDmLoaiYeuCauKichHoat()
        {
            List<Dm_LoaiYeuCau> listLoaiYeuCau = (from loaiYeuCau in entities.Dm_LoaiYeuCau
                                                  where loaiYeuCau.KichHoat == true
                                                  select loaiYeuCau).ToList();
            return listLoaiYeuCau;
        }

        public List<Dm_LoaiYeuCau> RetrieveAllDmLoaiYeuCauNgoaiTru()
        {
            List<Dm_LoaiYeuCau> listLoaiYeuCau = (from loaiYeuCau in entities.Dm_LoaiYeuCau
                                                  where loaiYeuCau.KichHoat == true
                                                        && (loaiYeuCau.DungChoNoiNgoaiTru == 0 || loaiYeuCau.DungChoNoiNgoaiTru == 1)
                                                  select loaiYeuCau).ToList();
            return listLoaiYeuCau;
        }

        public List<Dm_LoaiYeuCau> RetrieveAllDmLoaiYeuCauNoiTru()
        {
            List<Dm_LoaiYeuCau> listLoaiYeuCau = (from loaiYeuCau in entities.Dm_LoaiYeuCau
                                                  where loaiYeuCau.KichHoat == true
                                                        && (loaiYeuCau.DungChoNoiNgoaiTru == 0 || loaiYeuCau.DungChoNoiNgoaiTru == 2) 
                                                  select loaiYeuCau).ToList();
            return listLoaiYeuCau;
        }

        public void SaveLoaiYeuCau(string maLoaiYeuCau, string tenLoaiYeuCau, string ghiChu, int soDongIn
                                        , short stt, byte dungCho, bool kichHoat, bool themMoi)
        { 
            if(themMoi)
            {
                Dm_LoaiYeuCau dmLoaiYeuCau = new Dm_LoaiYeuCau();
                dmLoaiYeuCau.MaLoaiYeuCau = maLoaiYeuCau;
                dmLoaiYeuCau.TenLoaiYeuCau = tenLoaiYeuCau;
                dmLoaiYeuCau.GhiChu = ghiChu;
                dmLoaiYeuCau.SoDongInPhieu = soDongIn;
                dmLoaiYeuCau.STT = stt;
                dmLoaiYeuCau.KichHoat = kichHoat;
                dmLoaiYeuCau.DungChoNoiNgoaiTru = dungCho;
                entities.AddToDm_LoaiYeuCau(dmLoaiYeuCau);
                entities.SaveChanges();
            }
            else
            {
                Dm_LoaiYeuCau updateLoaiYC = entities.Dm_LoaiYeuCau.Where(u => u.MaLoaiYeuCau == maLoaiYeuCau).First();
                updateLoaiYC.MaLoaiYeuCau = maLoaiYeuCau;
                updateLoaiYC.TenLoaiYeuCau = tenLoaiYeuCau;
                updateLoaiYC.GhiChu = ghiChu;
                updateLoaiYC.SoDongInPhieu = soDongIn;
                updateLoaiYC.STT = stt;
                updateLoaiYC.KichHoat = kichHoat;
                updateLoaiYC.DungChoNoiNgoaiTru = dungCho;
                entities.SaveChanges();
            }
        }

        public int SoLoaiYeuCauKhongInNgoaiTru()
        {
            return (from u in entities.Dm_LoaiYeuCau where (u.DungChoNoiNgoaiTru != 1 && u.DungChoNoiNgoaiTru != 0) select u).Count();
        }

        public int SoLoaiYeuCauKhongInNoiTru()
        {
            return (from u in entities.Dm_LoaiYeuCau where (u.DungChoNoiNgoaiTru != 2 && u.DungChoNoiNgoaiTru != 0) select u).Count();
        }

        public bool WasUsedLoaiYeuCau(string maLoaiYeuCau)
        {
            return (entities.Dm_YeuCau.Where(u => u.Dm_LoaiYeuCau.MaLoaiYeuCau == maLoaiYeuCau).FirstOrDefault() != null ? true : false);
        }

        public void XoaLoaiYeuCau(string maLoaiYeuCau)
        {
            Dm_LoaiYeuCau xoaLoaiYC = entities.Dm_LoaiYeuCau.Where(u => u.MaLoaiYeuCau == maLoaiYeuCau).First();
            entities.DeleteObject(xoaLoaiYC);
            entities.SaveChanges();
        }

        public bool IsDuplicateLoaiYeuCau(string maLoaiYeuCau)
        {
            Dm_LoaiYeuCau loaiYeuCau = entities.Dm_LoaiYeuCau.Where(u => u.MaLoaiYeuCau == maLoaiYeuCau).FirstOrDefault();
            return loaiYeuCau != null ? true : false;
        }
    }
}
